//
//  mybuf.swift
//  touc
//
//  Created by z on 2020/6/26.
//  Copyright © 2020 z. All rights reserved.
//

import Foundation
class structWrapper
{
    var data:[UInt8]
    var pos=0
    init(_ d:[UInt8]=[UInt8]()) {
        self.data = d
    }
    
    func writeArr(_ a:[UInt8])
    {
        self.data += a
        self.pos += a.count
    }
    func readArr(_ l:Int) -> [UInt8]
    {
        var tt = self.pos
        self.pos += l
        if self.pos>data.count
        { self.pos = data.count}
        return [UInt8](self.data[tt..<self.pos])
    }
    func writeByte(_ b:Int)
    {
        var a = bytes(repeating: 0, count: 1)
        structPack(1, Int64(b), &a)
        self.data +=  a
        self.pos += 1
    }
    func writeWord(_ b:Int)
    {
        var a = bytes(repeating: 0, count: 2)
        structPack(2, Int64(b), &a)
        self.data +=  a
        self.pos += 2
    }
    func writeDWord(_ b:Int)
    {
        var a = bytes(repeating: 0, count: 4)
        structPack(4, Int64(b), &a)
        self.data +=  a
        self.pos += 4
    }
    func writeQ(_ b:Int64)
    {
        var a = bytes(repeating: 0, count: 8)
        structPack(8, Int64(b), &a)
        
        self.data +=  a
        self.pos += 8
    }
    func writeFloat(_ b:Float)
    {
        var a = bytes(repeating: 0, count: 4)
        structPack_d(4, Double(b), &a)
        self.data +=  a
        self.pos += 4
    }
    func writeDouble(_ b:Double)
    {
        var a = bytes(repeating: 0, count: 8)
        structPack_d(8, b, &a)
        self.data +=  a
        self.pos += 8
    }
    
    func readByte()->Int
    {
        var a = self.readArr(1)
        return Int(structUnpack(1, &a))
    }
    func readWord()->Int
    {
        var a = self.readArr(2)
        return Int(structUnpack(2, &a))
    }
    func readQ()->Int64
    {
        var a = self.readArr(8)
        return structUnpack(8, &a)
        
    }
    func readDouble()->Double
    {
        var a = self.readArr(8)
        return structUnpack_d(8, &a)
    }
    func getLeftData()->bytes{
        return self.readArr(self.leftLen())
    }
    
    func leftLen()->Int{
        return self.data.count-self.pos
    }
}
/*
 
 
 
 
 
 
 def writeQ(self,b):
 self.data += struct.pack('Q',b)
 self.pos += 8
 def writeFloat(self,b):
 self.data += struct.pack('f',b)
 self.pos += 4
 
 def readFloat(self):
 r = struct.unpack('f',self.data[self.pos:self.pos+4])[0]
 self.pos += 4
 return r
 
 def readByte(self):
 r = struct.unpack('B',self.data[self.pos:self.pos+1])[0]
 self.pos += 1
 return r
 
 
 def readDWord(self):
 r = struct.unpack('I',self.data[self.pos:self.pos+4])[0]
 self.pos += 4
 return r
 def readDouble(self):
 r = struct.unpack('d',self.data[self.pos:self.pos+8])[0]
 self.pos += 8
 return r
 
 def readQ(self):
 r = struct.unpack('Q',self.data[self.pos:self.pos+8])[0]
 self.pos += 8
 return r
 

 */
